﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OntologyCache;
using System.Text.RegularExpressions;
using System.Net;

namespace biolit_config
{
    /// <summary>
    /// Interaction logic for OntologyDownloadManagement.xaml
    /// </summary>
    public partial class OntologyDownloadManagement : Window
    {
        public OntologyDownloadManagement()
        {
            InitializeComponent();
            GetDownloadURLs();
            
        }

        private void CloseDownloadManagement(object sender, RoutedEventArgs e)
        {
            DownloadManagementDialog.Close();
        }

        private void AddDownloadLocationClick(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrEmpty(txtURL.Text))
            {
                txtName.Text = Regex.Replace(txtName.Text, @"[!@#$%_']", "");
                txtURL.Text = Regex.Replace(txtURL.Text, @"[!@#$%_']", "");
                if (URLCheck(txtURL.Text))
                {
                    ConfigSettings configSettings = Biolit_Settings.Settings;
                    List<OntologyDownloadLocation> lstDownloadLocations = configSettings.AvailableDownloadURLs.ToList();
                    OntologyDownloadLocation OntologyDownloadLoc = new OntologyDownloadLocation(txtName.Text, txtURL.Text);
                    lstDownloadLocations.Add(OntologyDownloadLoc);
                    configSettings.DownloadURLLocations = lstDownloadLocations.ToArray();
                    configSettings.AvailableDownloadURLs = lstDownloadLocations.ToArray();
                    configSettings.SelectedDownloadURL.Name = txtName.Text;
                    configSettings.SelectedDownloadURL.URL = txtURL.Text;
                    Biolit_Settings.StoreSettings();
                    txtName.Text = string.Empty;
                    txtURL.Text = string.Empty;
                    GetDownloadURLs();
                }
                else
                {
                    MessageBox.Show("Please enter Valid URL");
                    txtURL.Text = string.Empty;
                }
            }
            else
            {
                MessageBox.Show("Please enter Name, URL and Click Add");
            }
            

        }
        private void GetDownloadURLs()
        {
            ConfigSettings configSettings = Biolit_Settings.Settings;
            OntologiesSourceList.Items.Clear();
            foreach (OntologyDownloadLocation ontologyDownloadLoc in configSettings.AvailableDownloadURLs)
            {
                OntologiesSourceList.Items.Add(ontologyDownloadLoc);
            }
        }

        private void CancelDownloadManagement(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       

        private void btnDeleteurl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                MessageBoxResult objResult;
                objResult = MessageBox.Show("Are you sure to delete this item?", "Delete Confirmation", MessageBoxButton.OKCancel);
                if(objResult == MessageBoxResult.OK)
                {
                ConfigSettings configSettings = Biolit_Settings.Settings;
                OntologyDownloadLocation ontDownloadLocation = (OntologyDownloadLocation)((TextBlock)sender).DataContext;
                if (ontDownloadLocation != null)
                {
                    OntologiesSourceList.Items.Remove(ontDownloadLocation);
                    List<OntologyDownloadLocation> lstDownloadLocations = configSettings.AvailableDownloadURLs.ToList();
                    foreach (OntologyDownloadLocation downloadLocation in lstDownloadLocations)
                    {
                        if (downloadLocation.Name.Equals(ontDownloadLocation.Name, StringComparison.CurrentCultureIgnoreCase) && downloadLocation.URL.Equals(ontDownloadLocation.URL, StringComparison.CurrentCultureIgnoreCase))
                        {
                            lstDownloadLocations.Remove(downloadLocation);
                            break;
                        }
                    }
                    configSettings.DownloadURLLocations = lstDownloadLocations.ToArray();
                    configSettings.AvailableDownloadURLs = lstDownloadLocations.ToArray();
                    Biolit_Settings.StoreSettings();
                }
                }
            }
            catch (Exception)
            {
            }

        }

        private bool URLCheck(string url)
        {
            if (!String.IsNullOrEmpty(url))
            {
                bool validUrl = isValidUrl(url);
                if (validUrl)
                {
                    Uri urlCheck = new Uri(url);
                    WebRequest request = WebRequest.Create(urlCheck);
                    request.Timeout = 15000;
                    WebResponse response;
                    try
                    {
                        response = request.GetResponse();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            else
            {
                return false;
            }
        }

        public bool isValidUrl(string url)
        {
            string pattern = @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$";
            Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return reg.IsMatch(url);
        } 

    }
}
